home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / POSIX / ThinkCPosix / dup.c < prev    next >
Text File  |  1992-09-14  |  522b  |  25 lines

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * This implementation of dup() disables fd;
  5.  * in effect, it assumes that fd is closed
  6.  * immediately after dup() (as is almost always the case).
  7.  * The problem is that if __file[fd]->func() is left unchanged
  8.  * then close(fd) will also close dup(fd).
  9.  */
  10.  
  11. #include "ThinkCPosix.h"
  12.  
  13. int dup(int fd)
  14. {
  15.     FILE *fp1 = fdopen(fd, "");
  16.     FILE *fp2;
  17.     
  18.     if (fp1 == NULL)
  19.         return -1;
  20.     fp2 = __getfile();
  21.     memcpy((char*)fp2, (char*)fp1, sizeof(FILE));
  22.     memset((char*)fp1, 0, sizeof(FILE));
  23.     return fileno(fp2);
  24. }
  25.